#ifndef COMTHREAD_F210_H
#define COMTHREAD_F210_H

#include <QObject>

#include <QThread>
#include <QObject>
#include <QSerialPort>
#include <QTimer>

#define WAIT_STU_OUT  0
#define WAIT_STU_WAIT 1
#define WAIT_STU_DATA 2
#define WAIT_STU_REPT 3

class comthread_f210 : public QThread
{
    Q_OBJECT
public:
    explicit comthread_f210(QObject *parent = nullptr);

    QStringList GetSerialAvailable();
    bool InitSerial(QString portname);
    void CloseSerial();

    void RecvTime_Init(long DataSize);
    void SendSerial_LinkCmd_Link();
    void SendSerial_LinkCmd_Wait();
    void SendSerial_LinkCmd_Break();
    void SendSerial_LinkCmd_Repeat();
    void RecvSerial_LinkCmd_Repeat();

    bool RecvData_TimeOut(QByteArray data);

signals:
    void UpdateSerialData(QByteArray data);

public slots:
    void SerialPort_Read_Start();
    void ReceviceSerialData();
    void RespTimer_TimeOut();
    void WaitTimer_TimeStart();
    void WaitTimer_TimeStop();
    bool WaitTimer_TimeActive();
    void WaitTimer_TimeOut();
    void SendSerialData(QByteArray data);
    void SendSerial_MtsnCmd_SendCode(QByteArray code, uint16_t addr);
    void SendSerial_MtsnCmd_RecvCode(QByteArray code, uint16_t addr);
    void SendSerial_MtsnCmd_SendData_Start(QByteArray data, uint16_t addr);
    void SendSerial_MtsnCmd_SendData(QByteArray data, uint16_t addr);
    void SendSerial_MtsnCmd_SendData_End(QByteArray data, uint16_t addr);
    void SendSerial_MtsnCmd_RecvData_Start(QByteArray data, uint16_t addr);
    void SendSerial_MtsnCmd_RecvData(QByteArray data, uint16_t addr);
    void SendSerial_MtsnCmd_RecvData_End(QByteArray data, uint16_t addr);

    void SendSerial_Read_GammaData(QByteArray data, int index);
    void SendSerial_Write_GammaData(QByteArray data, int index);

private:
    int RespStu;
    QSerialPort *mSerial = nullptr;
    QTimer *RecvTimer = nullptr;
    long RecvTime;
    long RecvTimeOut;
    QTimer *RespTimer = nullptr;
    QTimer *WaitTimer = nullptr;
    QByteArray LastCmd;
};

#endif // COMTHREAD_F210_H
